package com.cloud.template.base.mvc;

import com.cloud.template.base.converter.DateConverter;
import com.cloud.template.base.interceptor.WebMvclerInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * mvc配置
 * Created by Viole on 2017/10/6.
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    /**
     * 自定义拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry){
        registry.addInterceptor(new WebMvclerInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/login");
    }

    /**
     * 格式转换注册
     */
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new DateConverter());
        super.addFormatters(registry);
    }

    /**
     * 资源文件注册
     * 设置静态资源的访问路径
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/js/**").addResourceLocations(
                "classpath:/static/js/");
        registry.addResourceHandler("/images/**").addResourceLocations(
                "classpath:/static/images/");
        registry.addResourceHandler("/css/**").addResourceLocations(
                "classpath:/static/css/");
        registry.addResourceHandler("/html/**").addResourceLocations(
                "classpath:/static/html/");
        super.addResourceHandlers(registry);
    }

}
